#include "year.h"

// See year.h
Year::Year(int const year, int const firstDay) {
    // Assign the year, and determine if we are a leap year
    this->year = year;
    
    // Create the months
    int tmpDay = firstDay;
    this->months = new Month::Month[NUM_MONTHS];
    for (int i=0; i<NUM_MONTHS; i++) {
        months[i].setMonthInYear(i+1, isLeapYear());
        months[i].generateDates(tmpDay);
        
        tmpDay = months[i].getLastDay();
        if (tmpDay == 7) { tmpDay = 1; }
        else { tmpDay++; }
    }
}

// See year.h
Year::~Year() {
    if (months) { delete [] months; }
}

// See year.h
Month::Month* Year::getMonth(int const month) {
    return &this->months[month];
}

// See year.h
bool Year::isLeapYear() {
    /*
    The year is evenly divisible by 4;
    If the year can be evenly divided by 100, it is NOT a leap year, unless;
    The year is also evenly divisible by 400. Then it is a leap year.
    */
    bool isLeapYear = false;
    if (this->year % 4 == 0) {
        isLeapYear = true;
        if (this->year % 100 == 0 && this->year % 400 != 0) {
            isLeapYear = false;
        }
    }
    return isLeapYear;
}
